<?php
/*
Plugin Name: 快速链接
Plugin URI: 
Description: 可以设置 /go/xxx/ 的快速链接，并且可以根据不同设备跳转到不同的链接。
Version: 1.0
*/

add_filter('wpjam_post_types', 'wpjam_add_go_post_type');
function wpjam_add_go_post_type($wpjam_post_types){
	$wpjam_post_types['go']  = array(
		'label'				=> '快速链接',
		'public'			=> true,
		'show_ui'			=> false,
		'has_archive'		=> false,
		// 'capability_type'	=> 'go',
		'menu_position'		=> 6,
		'menu_icon'			=> 'dashicons-admin-links',
		'rewrite'			=> false,
		'permastruct'		=> 'go/%post_id%/',
		'supports'			=> array(
			'title',
			'excerpt'
		)
	);
	return $wpjam_post_types;
}

add_filter('wpjam_post_options', 'wpjam_go_post_options');
function wpjam_go_post_options($wpjam_post_options){

	$wpjam_post_options['go-box'] = array(
		'title'			=>  '设置',
		'post_types'	=>  array('go'),
		'fields'		=>  array(
			'link'			=> array( 'title' => '链接',			'type' => 'textarea',	'rows'=>3,'description'=>'如果设置了iPhone和安卓连接，系统会根据不同的手机系统自动跳转。'),
			'link_iphone'	=> array( 'title' => 'iPhone 链接',	'type' => 'textarea',	'rows'=>3),
			'link_android'	=> array( 'title' => '安卓链接',		'type' => 'textarea',	'rows'=>3),
			'thumbnail'		=> array( 'title' => '缩略图',		'type' => 'image'),
		)
	);
	return $wpjam_post_options;
}

add_filter('wpjam_pre_post_thumbnail_uri','wpjam_go_post_thumbnail_uri',10,2);
function wpjam_go_post_thumbnail_uri($post_thumbnail_uri,$post){
	if($thumbnail = get_post_meta($post->ID,'thumbnail',true)){
		return $thumbnail;
	}else{
		return $post_thumbnail_uri;
	}
}


add_action('template_redirect', 'wpjam_go_template_redirect');
function wpjam_go_template_redirect(){
	if(is_single() && get_post_type()=='go'){
		if(($link_iphone  = get_post_meta(get_the_ID(),'link_iphone',true)) && is_iphone()) {
			wp_redirect( $link_iphone, '301' );
		}elseif(($link_android  = get_post_meta(get_the_ID(),'link_android',true)) && is_android()) {
			wp_redirect( $link_android, '301' );
		}elseif($link = get_post_meta(get_the_ID(),'link',true)){
			wp_redirect( $link, '301' );
		}else{
			wp_die('你必须设置导出链接');
		}
	}
}


		